package sjd.ahsj.springbootmybatisplusoracle.crontab;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sjd.ahsj.springbootmybatisplusoracle.entity.IDataDefine;
import sjd.ahsj.springbootmybatisplusoracle.entity.IFactory;
import sjd.ahsj.springbootmybatisplusoracle.service.IIDataDefineService;
import sjd.ahsj.springbootmybatisplusoracle.service.IIFactoryService;
import sjd.ahsj.springbootmybatisplusoracle.util.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 定时获取数据
 *
 * @author zhanghaogao
 * @create 2018-09-18 下午12:52
 **/
@Component
public class HistoryData {
    @Autowired
    private IIFactoryService iiFactoryService;
    @Autowired
    private IIDataDefineService iiDataDefineService;


    /**
     * 历史表数据同步
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void historyData() {
        List<IFactory> list = iiFactoryService.selectList(new EntityWrapper<>());
        for (IFactory info : list) {
            //获取电站历史表数据
            List<IDataDefine> historys = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq(
                    "factory_id", info.getId()).eq("history", 1));
            if (historys.size() > 0) {
                //拼接表一的查询sql
                String sql1 = "";
                String sql2 = "";
                for (IDataDefine iDataDefine : historys) {
                    if (iDataDefine.getHistory() == 1) {
                        if (Integer.parseInt(iDataDefine.getKey().substring(4, 6)) > 5) {
                            sql2 = sql2 + iDataDefine.getKey() + ",";
                        } else {
                            sql1 = sql1 + iDataDefine.getKey() + ",";
                        }

                    }
                }
                sql2 = sql2.substring(0, sql2.length() - 1);
                sql1 = sql1.substring(0, sql1.length() - 1);
                //查询实时表
                Map<String, Object> map = iiFactoryService.selectFactoryInfo(1, info.getId(), sql1);
                if (map != null) {
                    map.putAll(iiFactoryService.selectFactoryInfo(2, info.getId(), sql2));
                    //插入历史数据
                    iiFactoryService.insertHistoryInfo(info.getId(), map);
                }

            }
        }
    }


    /**
     * 绘图表5秒同步
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void plotyData() {
        Date date = new Date();
        List<IFactory> list = iiFactoryService.selectList(new EntityWrapper<>());
        for (IFactory info : list) {
            //获取绘图表数据
            List<IDataDefine> historys = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq(
                    "factory_id", info.getId()).eq("history", 0).like("key_", "PA"));
            if (historys.size() > 0) {
                //拼接表一的查询sql
                String sql1 = "";
                String sql2 = "";
                for (IDataDefine iDataDefine : historys) {
                    if (Integer.parseInt(iDataDefine.getKey().substring(4, 6)) > 5) {
                        sql2 = sql2 + iDataDefine.getKey() + ",";
                    } else {
                        sql1 = sql1 + iDataDefine.getKey() + ",";
                    }
                }
                sql2 = sql2.substring(0, sql2.length() - 1);
                sql1 = sql1.substring(0, sql1.length() - 1);
                //查询实时表
                Map<String, Object> map = iiFactoryService.selectFactoryInfo(1, info.getId(), sql1);
                if (map != null) {
                    map.putAll(iiFactoryService.selectFactoryInfo(2, info.getId(), sql2));
                    map.put("type", "1");
                    //插入数据
                    iiFactoryService.insertPlotInfo(info.getId(), map, date);
                } else {
                    map = Maps.newHashMap();
                    map.put("type", "1");
                    //插入数据
                    iiFactoryService.insertPlotInfo(info.getId(), map, date);
                }

            }
        }
    }


    /**
     * 绘图表15分钟同步
     */
    @Scheduled(cron = "0 0/15 * * * ?")
    public void plotyDataMinute() {
        //获取5分钟前的左右数据
        Date date = new Date();
        Date stDate = DateUtil.getSomeMinute(date, -15);
        List<IFactory> list = iiFactoryService.selectList(new EntityWrapper<>());
        for (IFactory info : list) {
            //获取绘图表数据
            List<IDataDefine> historys = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq(
                    "factory_id", info.getId()).eq("history", 0).like("key_", "PA"));
            if (historys.size() > 0) {
                //拼接表一的查询sql
                String sql = "";
                for (IDataDefine iDataDefine : historys) {
                    sql = sql + "AVG(" + iDataDefine.getKey() + ") as " + iDataDefine.getKey() + ",";
                }
                sql = sql.substring(0, sql.length() - 1);
                //查询绘图表历史数据
                Map<String, Object> map = iiFactoryService.selectPlotInfoBySql(info.getId(), sql, stDate, date, 1);
                if (map == null) {
                    map = Maps.newHashMap();
                }
                map.put("type", "2");
                //插入数据
                iiFactoryService.insertPlotInfo(info.getId(), map, date);
                //清空5分钟前的秒数据
                iiFactoryService.delPlotInfoByType(info.getId(), DateUtil.getSomeMinute(date, -5), 1);

            }
        }

    }

    /**
     * 绘图表6小时同步
     */
    @Scheduled(cron = "0 0 0/6 * * ?")
    public void plotyDataHour() {
        //获取5分钟前的左右数据
        Date date = new Date();
        Date stDate = DateUtil.getSomeMinute(date, -360);
        List<IFactory> list = iiFactoryService.selectList(new EntityWrapper<>());
        for (IFactory info : list) {
            //获取绘图表数据
            List<IDataDefine> historys = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq(
                    "factory_id", info.getId()).eq("history", 0).like("key_", "PA"));
            if (historys.size() > 0) {
                //拼接表一的查询sql
                String sql = "";
                for (IDataDefine iDataDefine : historys) {
                    sql = sql + "AVG(" + iDataDefine.getKey() + ") as " + iDataDefine.getKey() + ",";
                }
                sql = sql.substring(0, sql.length() - 1);
                //查询绘图表历史数据
                Map<String, Object> map = iiFactoryService.selectPlotInfoBySql(info.getId(), sql, stDate, date, 2);
                if (map == null) {
                    map = Maps.newHashMap();
                }
                map.put("type", "3");
                //插入数据
                iiFactoryService.insertPlotInfo(info.getId(), map, date);
                //清空750分钟前的秒数据
                iiFactoryService.delPlotInfoByType(info.getId(), DateUtil.getSomeMinute(date, -750), 2);

            }
        }

    }

    /**
     * 绘图表1月同步
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void plotyDataM() {
        //获取5分钟前的左右数据
        Date date = new Date();
        Date stDate = DateUtil.getSomeDay(date, 30);
        List<IFactory> list = iiFactoryService.selectList(new EntityWrapper<>());
        for (IFactory info : list) {
            //获取绘图表数据
            List<IDataDefine> historys = iiDataDefineService.selectList(new EntityWrapper<IDataDefine>().eq(
                    "factory_id", info.getId()).eq("history", 0).like("key_", "PA"));
            if (historys.size() > 0) {
                //拼接表一的查询sql
                String sql = "";
                for (IDataDefine iDataDefine : historys) {
                    sql = sql + "AVG(" + iDataDefine.getKey() + ") as " + iDataDefine.getKey() + ",";
                }
                sql = sql.substring(0, sql.length() - 1);
                //查询绘图表历史数据
                Map<String, Object> map = iiFactoryService.selectPlotInfoBySql(info.getId(), sql, stDate, date, 3);
                if (map == null) {
                    map = Maps.newHashMap();
                }
                map.put("type", "4");
                //插入数据
                iiFactoryService.insertPlotInfo(info.getId(), map, date);
                //清空15天前的小时数据
                iiFactoryService.delPlotInfoByType(info.getId(), DateUtil.getSomeDay(date, -15), 3);
            }
        }

    }
}
